package net.xxcxcxcx.xc_enhancedmob.skills.basics.individual.common;

import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.projectile.thrown.PotionEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.potion.PotionUtil;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.xxcxcxcx.xc_enhancedmob.config.skill.SkillConfig;
import net.xxcxcxcx.xc_enhancedmob.skills.BaseSkill;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class MedicalTreatmentSkill extends BaseSkill {

    public MedicalTreatmentSkill(int level) {
        super(level);
    }

    public MedicalTreatmentSkill(NbtCompound nbtCompound) {
        super(nbtCompound);
    }

    public MedicalTreatmentSkill() {
    }

    @Override
    public SkillConfig getDefaultConfig() {
        SkillConfig skillConfig = super.getDefaultConfig();
        skillConfig.setCooldown(180);
        skillConfig.setMessage(getConfigMessage(skillConfig));
        return skillConfig;
    }

    @Override
    public String getConfigMessage(SkillConfig skillConfig) {
        if (skillConfig == null) return super.getConfigMessage(null);
        return String.format("医疗技能，可以向同一仇恨的队友发射药水。随等级提升治疗量。冷却时间 %d tick。", skillConfig.getCooldown());
    }

    @Override
    public boolean canUseAbility(MobEntity mobEntity) {
        return (getSkilltime() < mobEntity.world.getTime());
    }

    @Override
    public boolean onAbilityUse(MobEntity mobEntity) {
        if (canUseAbility(mobEntity)) {
            setSkilltime(mobEntity.world.getTime() + getCooldown());
            int L = getSkillLevel() * 2 + 5;
            List<MobEntity> mobEntities = mobEntity.world.getEntitiesByClass(
                    MobEntity.class,
                    mobEntity.getBoundingBox().stretch(L, L / 2, L).stretch(-L, -L / 2, -L),
                    e -> e == mobEntity || (e.getTarget() == mobEntity.getTarget() && e.getTarget() != null)
            );
            mobEntities.sort((a, b) -> (a.getHealth() / a.getMaxHealth() - b.getHealth() / b.getMaxHealth()) == 0 ? 0 : ((a.getHealth() / a.getMaxHealth() - b.getHealth() / b.getMaxHealth()) > 0 ? 1 : -1));
            for (int i = 0; i < mobEntities.size(); i++) {
                MobEntity mob = mobEntities.get(i);
                Collection<StatusEffectInstance> statusEffectInstances = new ArrayList<>();
                Vec3d vec3d = mob.getVelocity();
                if (mob.isUndead()) {
                    statusEffectInstances.add(new StatusEffectInstance(StatusEffects.INSTANT_DAMAGE, getSkillLevel() / 3 + 1, getSkillLevel() - 1));
                } else {
                    statusEffectInstances.add(new StatusEffectInstance(StatusEffects.INSTANT_HEALTH, getSkillLevel() / 3 + 1, getSkillLevel() - 1));
                }
                PotionEntity potionEntity = new PotionEntity(mobEntity.world, mobEntity);
                potionEntity.setPos(potionEntity.getX(), potionEntity.getY() + 1, potionEntity.getZ());
                double d = mob.getX() + vec3d.x - potionEntity.getX();
                double e = mob.getEyeY() - 1.1 - potionEntity.getY();
                double f = mob.getZ() + vec3d.z - potionEntity.getZ();
                float g = MathHelper.sqrt((float) (d * d + f * f));
                potionEntity.setItem(PotionUtil.setCustomPotionEffects(new ItemStack(Items.SPLASH_POTION), statusEffectInstances));
                potionEntity.pitch -= -20.0f;
                potionEntity.setVelocity(d, e + (double) (g * 0.2f), f, 0.75f, 8.0f);
                mobEntity.world.spawnEntity(potionEntity);
                break;
            }
        }
        return true;
    }
}
